﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthPostAction("DS", "CreateDataService", "1.0")]
    public class CreateDataServiceAction : BaseAction<CreateDataServiceParams, CreateDataServiceResult>
    {
        protected override ActionResult<CreateDataServiceResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            CreateDataServiceParams opParams)
        {

            Guid categoryId = new Guid("{9AB56520-B2A9-4BCA-B85E-E19A3764B26D}");
            Guid typeId = new Guid("{D75C0354-4F07-4C2D-80B5-055B3105401E}");

            SER.DtSerService dtSerService = new SER.DtSerService();
            ServiceResult<ENT.DtSer> serResult = dtSerService.CreateDtSer(context.UserId, opParams.FolderId,
                opParams.Name,
                opParams.Address,
                true,
                opParams.AppId.ToString(),
                opParams.AppToken,
                categoryId,
                typeId,
                opParams.Description);
            if(serResult.IsSuccess)
            {
                CreateDataServiceResult result = new CreateDataServiceResult();
                result.Id = serResult.Data.Id;
                return new ActionResult<CreateDataServiceResult>(result);
            }
            else
            {
                return new ActionResult<CreateDataServiceResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class CreateDataServiceParams
    {
        [Parameter]
        public string Name { get; set; }

        [Parameter]
        public string Description { get; set; }

        public string Address { get; set; }

        [Parameter]
        public Guid FolderId { get; set; }

        /// <summary>
        /// 目标系统为当前系统分配的外部程序id（必填）
        /// </summary>
        public Guid AppId { get; set; }

        /// <summary>
        /// 目标系统为当前系统分配的外部程序token（必填）
        /// </summary>
        public string AppToken { get; set; }
    }

    public class CreateDataServiceResult
    {
        public Guid Id { get; set; }
    }
}
